package com.tsunami.auth.controller;

import com.tsunami.auth.service.auth.IServerService;
import com.tsunami.authority.dto.auth.ServerSaveDTO;
import com.tsunami.authority.dto.auth.ServerUpdateDTO;
import com.tsunami.authority.entity.auth.Server;
import com.tsunami.base.R;
import com.tsunami.exception.code.ExceptionCode;
import com.tsunami.log.annotation.SysLog;
import com.tsunami.user.annotation.LoginUser;
import com.tsunami.user.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname ServerController
 * @Description TODO
 * @Date 2021-11-30 17:04
 * @Created by lx
 */
@RestController
@RequestMapping("/server")
@Api(value = "Server", tags = "服務管理")
public class ServerController {
    @Autowired
    IServerService serverService;

    @GetMapping("page")
    @ApiOperation(notes = "服务列表（分页）",value = "服务列表")
    @SysLog("获取服务列表")
    public R list(Integer pageNum,
                  Integer pageSize,
                  String orderByColumn,
                  String isAsc,
                  Server data){
        return serverService.selectPageList(data);
    }

    @ApiOperation(value = "新增服务", notes = "新增服务")
    @PostMapping("/save")
    @SysLog("新增服务")
    public R save(@LoginUser SysUser user,@Validated ServerSaveDTO data){
        if(serverService.isExist(data.getServerCode())){
            return R.error().exCode(ExceptionCode.HAVE_CODE);
        }
        serverService.saveServer(user,data);
        return R.ok();
    }

    @ApiOperation(value = "修改服务", notes = "修改服务")
    @PutMapping("/update")
    @SysLog("修改服务")
    public R update(@LoginUser SysUser user,@Validated ServerUpdateDTO data){
        serverService.updateUser(user,data);
        return R.ok();
    }

    /**
     * 删除服务（先删除客户端）
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除服务", notes = "根据id物理删除服务")
    @DeleteMapping("/delete")
    @SysLog("删除服务")
    public R delete(@RequestParam("ids[]") List<Long> ids) {
        serverService.remove(ids);
        return R.ok();
    }

}
